﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing;

namespace MyChart
{
    /// <summary>
    /// 泛型简单图表控件（可运行时动态变更简单图表实例）
    /// </summary>
    public class GenericSimpleChart : SimpleChart
    {
        #region 字段

        private SimpleChart _chart;
        private SimpleChartType _type;

        private List<EventHandler<FusionChartDataBindingEventArgs>> _listDataBindingEventDelegates = new List<EventHandler<FusionChartDataBindingEventArgs>>();
        private List<EventHandler<SimpleChartDataBindedEventArgs>> _listDataBindedEventDelegates = new List<EventHandler<SimpleChartDataBindedEventArgs>>();
        private List<EventHandler<SimpleChartRenderingEventArgs>> _listRenderingEventDelegates = new List<EventHandler<SimpleChartRenderingEventArgs>>();

        #endregion

        #region 属性

        /// <summary>
        /// 获取或设置坐标系图表控件类型
        /// </summary>
        public SimpleChartType Type
        {
            get { return _type; }
            set
            {
                _type = value;
                InitInternalChart();
            }
        }

        #endregion

        #region 事件

        /// <summary>
        /// 绘制控件前事件
        /// </summary>
        public override event EventHandler<FusionChartDataBindingEventArgs> DataBinding
        {
            add { _listDataBindingEventDelegates.Add(value); }
            remove { _listDataBindingEventDelegates.Remove(value); }
        }

        /// <summary>
        /// 绘制控件前事件
        /// </summary>
        public override event EventHandler<SimpleChartDataBindedEventArgs> DataBinded
        {
            add { _listDataBindedEventDelegates.Add(value); }
            remove { _listDataBindedEventDelegates.Remove(value); }
        }

        /// <summary>
        /// 绘制控件前事件
        /// </summary>
        public override event EventHandler<SimpleChartRenderingEventArgs> Rendering
        {
            add { _listRenderingEventDelegates.Add(value); }
            remove { _listRenderingEventDelegates.Remove(value); }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 重写 System.Web.UI.Control 基类的 OnInit 初始化事件
        /// </summary>
        /// <param name="e">事件参数</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitInternalChart();
        }

        /// <summary>
        /// 初始化内部图表控件
        /// </summary>
        private void InitInternalChart()
        {
            _chart = (SimpleChart)ChartFactory.CreateChart(_type.ToString());
            CheckInternalChart();
        }

        /// <summary>
        /// 同步自身与内部图表控件的属性
        /// </summary>
        private void SetInternalChartProperty()
        {
            CheckInternalChart();

            _chart.ID = ID;
            _chart.ItemManager = ItemManager;
            _chart.ToolTextFormat = ToolTextFormat;
            _chart.DataTextField = DataTextField;
            _chart.DataValueField = DataValueField;
            _chart.DataSource = DataSource;
            _chart.Width = Width;
            _chart.Height = Height;
            _chart.Caption = Caption;
            _chart.SubCaption = SubCaption;
            _chart.Animation = Animation;
            _chart.IsSliced = IsSliced;
            _chart.Theme = Theme;
            _chart.ShowLabels = ShowLabels;
            _chart.ShowValues = ShowValues;
            _chart.NumberPrefix = NumberPrefix;
            _chart.NumberSuffix = NumberSuffix;
            _chart.Decimals = Decimals;
            _chart.MarginTop = MarginTop;
            _chart.MarginBottom = MarginBottom;
            _chart.MarginLeft = MarginLeft;
            _chart.MarginRight = MarginRight;
            _chart.BgColor = BgColor;
            _chart.ShowBorder = ShowBorder;
            _chart.BaseFont = BaseFont;
            _chart.ForeColor = ForeColor;

            //SimpleChart Property
            _chart.PercentMode = PercentMode;
            _chart.FormatNumber = FormatNumber;
            _chart.ShowPercentInToolTip = ShowPercentInToolTip;
        }

        /// <summary>
        /// 验证是否已定义内部控件
        /// </summary>
        private void CheckInternalChart()
        {
            if (_chart == null)
            {
                throw (new Exception("控件类型'" + _type.ToString() + "'未定义！"));
            }
        }

        /// <summary>
        /// 重写 FusionChart 基类的 RenderControl 绘制控件方法
        /// </summary>
        /// <param name="writer">html绘制输出流</param>
        public override void RenderControl(System.Web.UI.HtmlTextWriter writer)
        {
            //同步内部控件的事件处理句柄
            foreach (EventHandler<SimpleChartRenderingEventArgs> handler in _listRenderingEventDelegates)
            {
                _chart.Rendering += handler;
            }
            //同步内部控件的属性
            SetInternalChartProperty();

            //取消自身的绘制，改为调用内部控件的绘制方法
            _chart.RenderControl(writer);
        }

        /// <summary>
        /// 获取控件的 FusionChart 完整Xml配置字符串
        /// </summary>
        /// <returns>完整的Xml配置字符串</returns>
        public override string GetRenderHtml()
        {
            SetInternalChartProperty();
            return _chart.GetRenderHtml();
        }

        /// <summary>
        /// 执行控件数据绑定
        /// </summary>
        public override void DataBind()
        {
            base.DataBind();
        }

        #endregion
    }
}
